<?php
require_once ('ini_set.inc');
require_once ('LyException.php');
require_once ('db.php');
require_once ('ClassUtil.php');
require_once ('GenerateObj.php');
class Category extends GenerateObj{
	
	protected $fields=array("id","name","parent_id","type_code","weight","create_time");
	protected $table_name="category";
	public $id;
	public $name;
	public $parent_id;
	public $type_code;
	public $weight;
	public $create_time;
	
	public function __construct(){
		$args=func_get_args();
		ClassUtil::callMethodForArgs($this,$args);
	}
	public function construct_(){
	}
	public  function construct_number($num){
		$this->id=$num;
		$this->generate_obj_by_field("id",$num);
	}
	public  function construct_string_number_number_number($name,$parent_id,$type_code,$weight){
		$this->name=$name;
		$this->parent_id=$parent_id;
		$this->type_code=$type_code;
		$this->weight=$weight;
	}
	
	private function db_save_category($name,$parent_id,$type_code,$weight){
		$sql="insert into category (name,parent_id,type_code,weight) values ('$name','$parent_id','$type_code','$weight')";
		$query = db_query($sql, "users", "write");
		if(!mysql_insert_id()) throw new LyException("save category fails");
		return mysql_insert_id();
	}
	public function save(){
		$id=$this->db_save_category($this->name,$this->parent_id,$this->type_code,$this->weight);
		//generate_user_by_field("id",$id);
		$this->id=$id;
		$this->create_time=date("Y-m-d H:i:s");
	}
}
?>